package com.ws.lv.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.lv.bean.Acl_permission;
import com.ws.lv.bean.Acl_role;
import com.ws.lv.bean.Acl_role_permission;
import com.ws.lv.bean.Acl_user;
import com.ws.lv.service.Acl_permissionService;
import com.ws.lv.service.Acl_roleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/acl_role")
public class Acl_roleController {
    @Autowired
    Acl_roleService acl_roleService;
    @Autowired
    Acl_permissionService acl_permissionService;

    //查询角色
    @RequestMapping("/selectAcl_roles")
    public String selectAcl_roles(String role_name, Model model, Integer currentPage) {
        PageHelper pageHelper = new PageHelper();//分页帮助类
        if (currentPage == null) {
            pageHelper.startPage(1, 5);
        } else {
            pageHelper.startPage(currentPage, 5);
        }
        List<Acl_role> aclRoles = acl_roleService.selectAcl_roles(role_name);
        PageInfo pageInfo = new PageInfo(aclRoles);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("role_name", role_name);
        model.addAttribute("currentPage", currentPage);

        return "adminpage/acl_role/list";
    }

    //去添加角色页面
    @RequestMapping("/toAddAcl_role")
    public String toAddAcl_role() {
        System.out.println("去添加角色页面");
        return "adminpage/acl_role/add";
    }

    //添加角色
    @RequestMapping("/insertAcl_role")
    public String insertAcl_role(Acl_role acl_role) {
        System.out.println("添加角色");
        int i = acl_roleService.insertAcl_role(acl_role);
        return "redirect:/acl_role/selectAcl_roles";
    }

    //逻辑删除角色
    @RequestMapping("/softDeleteAcl_role")
    public String softDeleteAcl_role(Integer role_id) {
        System.out.println("逻辑删除角色");
        int i = acl_roleService.softDeleteAcl_role(role_id);
        return "redirect:/acl_role/selectAcl_roles";
    }

    //去修改角色页面
    @RequestMapping("/toUpdateAcl_role")
    public String toUpdateAcl_role(Integer role_id, Model model) {
        System.out.println("去修改角色页面");
        Acl_role acl_role = acl_roleService.selectAcl_roleById(role_id);
        model.addAttribute("acl_role", acl_role);
        return "adminpage/acl_role/update";
    }

    //修改角色
    @RequestMapping("/updateAcl_role")
    public String updateAcl_role(Acl_role acl_role) {
        System.out.println("修改角色");
        int i = acl_roleService.updateAcl_role(acl_role);
        return "redirect:/acl_role/selectAcl_roles";
    }

    //批量删除角色
    @RequestMapping("/deleteAcl_roles")
    public String deleteAcl_roles(String role_ids) {
        System.out.println("批量删除角色");
        if (role_ids != null) {
            String[] split = role_ids.split(",");
            ArrayList<Integer> list = new ArrayList<>();
            for (String s : split) {
                list.add(Integer.parseInt(s));
            }
            for (Integer id : list){
                //删除连表信息
                acl_roleService.deleteAcl_role_permission(id);
            }
            //删除角色
            int i = acl_roleService.deleteAcl_roles(list);
        }
        return "redirect:/acl_role/selectAcl_roles";
    }

    //去分配权限页面
    @RequestMapping("/selectAcl_permissions")
    public String selectAcl_permissions(Integer role_id, Model model) {
        System.out.println("去分配权限页面");
        List<Acl_permission> acl_permissions = acl_permissionService.selectAcl_permissions(null, null);
        List<Acl_role_permission> Acl_role_permissions = acl_roleService.selectAcl_role_permissionByRoleId(role_id);
        for (Acl_permission acl_permission : acl_permissions){
            for (Acl_role_permission acl_role_permission : Acl_role_permissions){
                if (acl_permission.getPerm_id().equals(acl_role_permission.getPerm_id())){
                    acl_permission.setChecked(true);

                }
            }
            if (acl_permission.getChildren().size() > 0){
                for (Acl_permission children : acl_permission.getChildren()) {
                    for (Acl_role_permission acl_role_permission : Acl_role_permissions){
                        if (children.getPerm_id().equals(acl_role_permission.getPerm_id())){
                            children.setChecked(true);
                        }
                    }
                }
            }
        }

        model.addAttribute("acl_permissions", acl_permissions);
        model.addAttribute("role_id", role_id);
        return "adminpage/acl_role/menu-list";
    }

    //分配权限
    @RequestMapping("/insertAcl_role_permission")
    public String insertAcl_role_permission(Integer role_id, String perm_ids, HttpServletRequest request, HttpServletResponse response) throws IOException {
        System.out.println("分配权限");
        if (perm_ids != null && perm_ids.trim().length() > 0) {
            String[] split = perm_ids.split(",");
            ArrayList<Integer> list = new ArrayList<>();
            for (String s : split) {
                list.add(Integer.parseInt(s));
            }
            acl_roleService.deleteAcl_role_permission(role_id);

            for (Integer perm_id : list){
                int i = acl_roleService.insertAcl_role_permission(role_id, perm_id);
            }
        }else {
            acl_roleService.deleteAcl_role_permission(role_id);
        }
        Acl_user aclUser = (Acl_user) request.getSession().getAttribute("aclUser");
        List<Acl_role> aclRoles = acl_roleService.selectAcl_rolesByUserId(aclUser.getUser_id());

        for (Acl_role aclRole : aclRoles){
            if (aclRole.getRole_id().equals(role_id)){
                PrintWriter writer = response.getWriter();
                writer.write("<script>window.parent.location.href='"+request.getContextPath()+"/acl_user/outLogin'</script>");
                writer.close();
            }
        }

        return "redirect:/acl_role/selectAcl_roles";




    }

}